@extends('layouts.app')
@include('order.sidebar')
@section('content')
    @php
        use Illuminate\Support\Facades\Auth;
        use App\Modules\User\Models\Admin;
        $user = Auth::user();
    @endphp
    <div class="content">
        <div class="row custom-invoice">
            <div class="row">
                <form method="get" class="form form-inline" action="{{ route('admin.order.good_index') }}" autocomplete="off">
                    <div class="form-group" style="margin-left:15px;">
                        <input class="form-control" type="text" name="order_id" value="{{ $orderId }}" placeholder="订单ID">
                    </div>
                    <div class="form-group" style="margin-left:15px;">
                        <input class="form-control" type="text" name="name" value="{{ $name }}" placeholder="姓名">
                    </div>
                    <div class="form-group" style="margin-left:15px;">
                        <input class="form-control" type="text" name="mobile" value="{{ $mobile }}" placeholder="电话号">
                    </div>
                    <div class="form-group" style="margin-left:15px;">
                        <input class="form-control" type="text" name="goods_name" value="{{ $goodsName }}" placeholder="商品">
                    </div>
                    <div class="form-group" style="margin-left:15px;">
                        <input class="form-control" type="text" name="referrer_name" value="{{ $referrerName }}" placeholder="推荐人">
                    </div>
                    <div class="form-group" style="margin-left:15px;">
                        <label for="shop_id">门店: </label>
                        <select class="form-control select2" name="shop_id" id="shop_id">
                            @if ($shopId != '')
                                <option value="{{$shopId}}">{{$shopName}}</option>
                            @else
                                <option value="">选择门店</option>
                            @endif
                        </select>
                    </div>
                    <br>

                    <div class="form-group" style="margin-left:15px;margin-top: 15px;">
                        <div class="form-group form-inline">
                            <label for="delivery">邮递信息: </label>
                            <select class="form-control" name="delivery">
                                <option value="" {{ $delivery == ""? "selected":"" }}>全部</option>
                                <option value="0" {{ $delivery == "0"? "selected":"" }}>不需要物流</option>
                                <option value="1" {{ $delivery == "1"? "selected":"" }}>未发货</option>
                                <option value="2" {{ $delivery == "2"? "selected":"" }}>已发货</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group" style="margin-left:15px;margin-top: 15px;">
                        <div class="form-group form-inline">
                            <label for="delivery">订单类型: </label>
                            <select class="form-control" name="is_prepay">
                                <option value="" {{ $isPrepay == ""? "selected":"" }}>全部</option>
                                <option value="0" {{ $isPrepay == "0"? "selected":"" }}>全款订单</option>
                                <option value="1" {{ $isPrepay == "1"? "selected":"" }}>预付订单</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group" style="margin-left:15px;margin-top: 15px;">
                        <div class="form-group form-inline">
                            <label for="status">订单状态: </label>
                            <select class="form-control" name="status">
                                <option value="" {{ $status == ""? "selected":"" }}>全部</option>
                                <option value="pending" {{ $status == "pending"? "selected":"" }}>待支付</option>
                                <option value="prepaid" {{ $status == "prepaid"? "selected":"" }}>定金支付未完成</option>
                                <option value="paid" {{ $status == "paid"? "selected":"" }}>完成未交货</option>
                                <option value="need_comment" {{ $status == "need_comment"? "selected":"" }}>收货未评价</option>
                                <option value="refund" {{ $status == "refund"? "selected":"" }}>已退款</option>
                                <option value="close" {{ $status == "close"? "selected":"" }}>已关闭</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group" style="margin-left:15px;margin-top: 15px;">
                        <div class="form-group form-inline">
                            <label>时间范围:</label>
                            <input class="form-control" type="text" placeholder="起始时间" id = "start" name="start" value="{{ $start }}"/>
                            -
                            <input class="form-control" type="text" placeholder="截止时间" id = "end" name="end" value="{{ $end }}" />
                        </div>
                    </div>
                    <div class="form-group" style="margin-left:15px;margin-top: 15px;">
                        <div class="form-group">
                            <button class="btn btn-success btn-ms" type="submit">搜索</button>
                            @if($user->is_merchant_staff == Admin::MERCHANT_AUTH)
                                @can('merchant_staff_order_export')
                                    <button class="btn btn-primary btn-ms" type="submit" name="export" value="1">导出</button>
                                @endcan
                            @else
                                @can('goods-order-export')
                                    <button class="btn btn-primary btn-ms" type="submit" name="export" value="1">导出</button>
                                @endcan
                            @endif
                        </div>
                    </div>
                </form>
            </div>
            <div class="form-inline" style="margin-top:30px;">
                <div class="form-group" style="font-weight: bold; margin-right: 25px;">订单数量: {{ $count  }}</div>
                <div class="form-group" style="font-weight: bold; margin-right: 25px;">订单价格: {{ $original_price  }}</div>
                <div class="form-group" style="font-weight: bold; margin-right: 25px;">微信支付: {{ $we_chat_pay  }}</div>
                <div class="form-group" style="font-weight: bold; margin-right: 25px;">优惠券: {{ $coupon_pay  }}</div>
                <div class="form-group" style="font-weight: bold; margin-right: 25px;">退款金额: {{ $refund_fee  }}</div>
            </div>
            <hr />
            @foreach($data as $item)
                <div class="row" style="border-bottom: 2px solid #f4f4f4; margin-top: 50px;padding: 20px; background-color: #f4f4f4">
                    <div class="row" style="position: relative; height: 90px">
                        <div class="col-md-1 col-sm-6 col-xs-12" style="height: 100%">
                            <div style="position: absolute; top: 50%; transform: translate(0, -50%)">
                                <p><img src="{{ $item->avatar }}" width="40"></p>
                                <p>{{ $item->nickname }}</p>
                            </div>
                        </div>
                        <div class="col-md-2 col-sm-6 col-xs-12" style="height: 100%">
                            <div style="position: absolute; top: 50%; transform: translate(0, -50%)">
                                <p><span style="font-weight: bold">订单ID：</span>{{ $item->id }}</p>
                                <p><span style="font-weight: bold">订单价格：</span>{{ $item->original_price/100 }}</p>
                                <p><span style="font-weight: bold">下单时间: </span> {{ $item->created_at }}</p>
                            </div>
                        </div>
                        <div class="col-md-2 col-sm-6 col-xs-12"  style="height: 100%">
                            <div style="position: absolute; top: 50%; transform: translate(0, -50%)">
                                <p>{{ $item->is_prepay? "预付订单" : "全款订单" }}</p>
                                <p><span style="font-weight: bold">推荐人：</span>{{ $item->referrer_name }}</p>
                                <p><span style="font-weight: bold">店员：</span>{{ $personal->get($item->personal_id) ? $personal->get($item->personal_id)->name : "" }}</p>
                            </div>
                        </div>
                        <div class="col-md-2 col-sm-6 col-xs-12" style="height: 100%">
                            <div style="position: absolute; top: 50%; transform: translate(0, -50%)">
                                @if($item->is_prepay)
                                    <p>{{ \App\Modules\Order\Models\Order::$statusShow[$item->status] }} {{ $item->final_payment ? "已收尾款" : "未收尾款"}}</p>
                                @else
                                    <p>{{ \App\Modules\Order\Models\Order::$statusShow[$item->status] }}</p>
                                @endif
                                <p><span style="font-weight: bold">微信支付：</span>{{ $item->we_chat_pay/100 }}</p>
                                <p><span style="font-weight: bold">优惠券：</span>{{ $item->coupon_pay/100 }}</p>
                            </div>
                        </div>
                        <div class="col-md-3 col-sm-6 col-xs-12" style="height: 100%">
                            <div style="position: absolute; top: 50%; transform: translate(0, -50%)">
                                <p>{{ \App\Modules\Order\Models\Order::$deliveryShow[$item->delivery]}}</p>
                                <p><span style="font-weight: bold">姓名：</span>{{ $item->name }}</p>
                                <p><span style="font-weight: bold">电话：</span>{{ $item->mobile }}</p>
                                @if ($item->delivery > 0)
                                    <p><span style="font-weight: bold">地址：</span> {{ $item->address }}</p>
                                @endif
                            </div>
                        </div>
                        <div class="col-md-2 col-sm-6 col-xs-12" style="height: 100%">
                            <div style="position: absolute; top: 50%; transform: translate(0, -50%)">
                                @if($item->status == \App\Modules\Order\Models\Order::STATUS_PAID)
                                    @can('goods-order-final')
                                    @if(!$item->final_payment && $item->status == App\Modules\Order\Models\Order::STATUS_PAID)
                                        <a class="btn btn-primary btn-xs" href="{{ route('admin.order.final_payment', ['id' => $item->id]) }}"><i class="fa fa-star"></i> 收尾款</a>
                                    @endif
                                    @endcan
                                    @can('goods-order-refund')
                                    @if(!$item->buyer_confirm)
                                        <a class="btn btn-danger btn-xs refund" data-url="{{ route("admin.order.refund", ['id' => $item->id]) }}"><i class="fa fa-money"></i> 退款</a>
                                    @endif
                                    @endcan
{{--                                    @if($item->delivery == 1)--}}
{{--                                        <a class="btn btn-info btn-xs" href=""><i class="fa fa-star"></i> 发货</a>--}}
{{--                                    @endif--}}
                                    @can('goods-order-finish')
                                    @if(!$item->is_finish)
                                        <a class="btn btn-success btn-xs" href="{{ route('admin.order.finish', ['id' => $item->id]) }}"><i class="fa fa-star"></i> 标记完成</a>
                                    @endif
                                    @endcan
                                    @can('goods-order-change')
                                    @if($item->status == \App\Modules\Order\Models\Order::STATUS_INIT)
                                        <a class="btn btn-danger btn-xs change-price" data-url="{{ route("admin.order.change_price", ['id' => $item->id]) }}"><i class="fa fa-money"></i> 修改价格</a>
                                    @endif
                                    @endcan
                                    @if(!empty($item->bill))
                                        <a class="btn btn-info btn-xs show-bill" data-image="{{ $item->bill }}"><i class="fa fa-file-image-o"></i> 查看凭证</a>
                                    @endif
                                @endif
                                @can('goods-order-change')
                                @if($item->status == \App\Modules\Order\Models\Order::STATUS_CLOSE)
                                    <a class="btn btn-danger btn-xs delete-confirm" data-href="{{ route('admin.order.delete', ['id' => $item->id]) }}"><i class="fa fa-trash-o"></i> 删除</a>
                                @endif
                                @endcan
                            </div>
                        </div>
                    </div>
                    <div class="row" style="margin-top: 20px;">
                        @foreach($item->detail as $value)
                            <?php
                                $param = "";
                                foreach ($value['param'] as $key => $tmp) {
                                    $param .= $key.":".$tmp." ";
                                }
                            ?>
                            <div class="col-md-10 col-sm-10 col-xs-12">
                                <div class="info-box" style="background-color: #64d5ca ">
                                    <img class="info-box-icon" src="{{ $value['order_cover'] }}"/>
                                    <div class="info-box-content" >
                                        <span class="info-box-text">{{ $value['good_name'] }}</span>
                                        <span class="progress-description">{{ $param }}</span>
                                    </div>
                                    <div class="info-box-content" >
                                        {{ ($value['price']/100)." * ". $value['number']}}
                                    </div>
                                </div>
                            </div>
                        @endforeach
                        @if(!empty($item->gift))
                        <div class="col-md-10 col-sm-10 col-xs-12">
                            @foreach($item->gift as $gift1)
                                @if(!empty($gift1))
                                    <div class="info-box" style="background-color: #64d5ca ">
                                        <div class="info-box-content" >
                                            @foreach($gift1 as $gift2)
                                                @if(!empty($gift2['content']))
                                                    @foreach($gift2['content'] as $gift_content)
                                                        <p>{{$gift_content['name']}}  {{$gift_content['price']/100}} * {{$gift_content['number']}}</p>
                                                    @endforeach
                                                @endif
                                            @endforeach
                                        </div>
                                    </div>
                                @endif
                            @endforeach
                        </div>
                        @endif
                        
                    </div>
                    
                    @if(!empty($item->buyer_remark))
                        <br />
                        <div class="col-md-10 col-sm-10 col-xs-10" style="height: 100%">
                            <div style="position: absolute; top: 50%; transform: translate(0, -50%)">
                                <p><span style="font-weight: bold">备注：</span>{{ $item->buyer_remark }}</p>
                            </div>
                        </div>
                    @endif
                </div>
            @endforeach
            {{ $data->appends(request()->all())->links() }}
        </div>
        <div class="modal fade loading custom-hide" id="hideForm" tabindex="-1" role="dialog" aria-labelledby="hideFormLibel" aria-hidden="true" data-backdrop="static" data-keyboard="false">
            <div class="modal-dialog">
                <div class="content invoice" style="text-align: left">
                    <form class='form'  action="" method="POST">
                        <input type="hidden" name="_token" value="{{ csrf_token() }}" />
                        <div class="form-group">
                            <label for="money">退款金额：</label>
                            <input class="form-control" type = "text" name = "money" id = "money" required />
                        </div>
                        <div class="form-group">
                            <label for="reason">退款理由：</label>
                            <input class="form-control" type = "text" name = "reason" id = "reason" required />
                        </div>
                        <div class="form-group">
                            <button type="submit" class="btn btn-success form-control">提交</button>
                            <br />
                            <br />
                            <button type="button" class="btn" id="closeHideForm">关闭</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>

        <div class="modal fade loading custom-hide"  style="width: 100%;text-align: center" id="hideForm2" tabindex="-1" role="dialog" aria-labelledby="hideFormLibel" aria-hidden="true" data-backdrop="static" data-keyboard="false">
            <div  class="content invoice" style="position: relative; width: 60%; margin: auto">
                <span id="closeHideForm2" class="span-X"><i class="fa fa-close"></i></span>
                <div class="row" id = "image-content">
                </div>
            </div>
        </div>
    </div>
@endsection
@push('stylesheets')
    <style>
        .span-X {
            position: absolute;
            border-radius: 50px;
            top: 10px;
            right: 10px;
            padding: 3px 5px;
            line-height: 0;
            background-color: rgba(190,190,190,0.50);
        }
    </style>
@endpush
@push('scripts')
    <script>

        $(".select2#shop_id").select2({
            width:"250px",
            ajax: {
                dataType: 'json',
                delay: 250,
                url: function () {
                    return '{{ route('admin.order.search_shops', ['is_photography' => 0]) }}';
                },
                processResults: function (data) {
                    return {
                        results: $.map(data, function (row, key) {
                            return {
                                id: key,
                                text: row,
                            };
                        })
                    };
                }
            }
        });

        $('#start').datetimepicker({format: "Y-m-d",timepicker:false});
        $('#end').datetimepicker({format: "Y-m-d",timepicker:false});
        // $('#start').attr('readonly', 'readonly');
        // $('#end').attr('readonly', 'readonly');
        $.datetimepicker.setLocale('zh');

        $('.refund').on('click', function () {
            $("#hideForm form").attr('action', $(this).attr('data-url'));
            $("#hideForm").modal('show');
        });
        $('#closeHideForm').on('click', function () {
            $("#hideForm").modal('hide');
        });

        $('.change-price').on('click', function () {
            $("#hideForm form").attr('action', $(this).attr('data-url'));
            $("#hideForm").modal('show');
        });

        $('.show-bill').on('click', function () {
            var k = $(this).attr('data-image').split(";");
            node = "";
            for(index in k) {
                node += "<div style= \"float:left;width: 500px;margin: 10px;\">\n" +
                    "       <img src=\"" + k[index] + "\" width='100%'>\n" +
                    "   </div>";
            }
            $('#image-content').html(node);
            $("#hideForm2").modal('show');
        });
        $('#closeHideForm2').on('click', function () {
            $("#hideForm2").modal('hide');
        });
    </script>
@endpush


